#!/usr/bin/python
# Copyright (C) 2003, Charles Wang.
# Author:  Charles Wang <charles@linux.net.cn>
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANT; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public LIcense for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.

import copy
import os
import string

class kmodule:
    def __init__(self, path, deps = []):
        self.path = path
        self.deps = copy.copy(deps)

    def __str__(self):
        return self.path

    def adddeps(self, deps = []):
        self.deps.extend(filter(lambda d: not d in self.deps, deps))

    def fulldeps(self):
        result = {}
        for d in self.deps:
            result[d] = 'y'
            for dd in d.deps:
                result[dd] = 'y'
        return  result.keys()

    def basename(self):
        return  os.path.basename(self.path)

class kmdict:
    def __init__(self):
        self.kmdict = {}
        self.kmbn2path = {}

    def __getitem__(self, key):
        try:
            return  self.kmdict[key]
        except KeyError:
            if len(self.kmbn2path[key]) > 1:
                raise KeyError, '%s => %s, Use path please' % (key, self.kmbn2path[key])
            return  self.kmdict[self.kmbn2path[key][0]]

    def loaddeps(self, f, kernelver):
        l = f.readline()
        while l:
            l = string.strip(l)
            deped = None
            if kernelver[:3] > '2.4':   # 2.6
                deped, deps = l.split(':', 1)
                deps = deps.split()
            else:                       # 2.4
                while len(l) > 0 and l[-1] == '\\':
                    la = string.strip(f.readline())
                    if not la:
                        break
                    l = l[:-1] + ':' + la
                if l:
                    kmpathes = filter(lambda kmpath: kmpath,
                                      map(lambda kmpath: string.strip(kmpath),
                                          string.split(l, ':')))
                    deped = kmpathes[0]
                    deps = kmpathes[1:]
            if deped:
                deped_bn = os.path.splitext(os.path.basename(deped))[0]
                if self.kmbn2path.has_key(deped_bn):
                    self.kmbn2path[deped_bn].append(deped)
                else:
                    self.kmbn2path[deped_bn] = [deped]
                for dep in filter(lambda dep: not self.kmdict.has_key(dep), deps):
                    self.kmdict[dep] = kmodule(dep)
                depsobj = map(lambda dep: self.kmdict[dep], deps)
                if self.kmdict.has_key(deped):
                    self.kmdict[deped].adddeps(depsobj)
                else:
                    self.kmdict[deped] = kmodule(deped, depsobj)
            l = f.readline()

if __name__=='__main__':                # A test program
    kernelver = os.uname()[2]
    kd = kmdict()
    f = open('/lib/modules/' + kernelver + '/modules.dep')
    kd.loaddeps(f, kernelver)
    f.close()
    for k in kd.kmdict:
        print kd.kmdict[k].path, map(lambda d: d.path, kd.kmdict[k].deps)
        print 'fulldeps = ['
        for dep in kd.kmdict[k].fulldeps():
            print '  %s, ' % str(dep)
        print ']'
        print '-' * 20
